﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using LPV_POS.Module;
using System.Resources;
using System.Globalization;

namespace LPV_POS.mh_pos.mh_cauhinh
{
    public partial class mh_banhang_dangnhap : DevExpress.XtraEditors.XtraForm
    {
        string str_thongbao= "";
        string str_lang = "";
        CultureInfo culture;

        #region "Muti language"

        private void SetLanguage_banhang(string cultureName)
        {
            try
            {
                culture = CultureInfo.CreateSpecificCulture(cultureName);
                ResourceManager rm = new ResourceManager("LPV_POS.Muti_language.MyResource", typeof(mh_banhang6_PhuThu).Assembly);
                //lblVAT_Co.Text = rm.GetString("vat", culture);
                //lblVAT_Khong.Text = rm.GetString("khongvat", culture);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Note");
            }

        }

        #endregion

        public mh_banhang_dangnhap()
        {
            InitializeComponent();
            culture = CultureInfo.CurrentCulture;
        }

        private void mh_dangnhap_Load(object sender, EventArgs e)
        {
            cpublic ocpublic = new cpublic();
             str_lang = ocpublic.Registry_Get("mh_main_lang");
            if (str_lang == "")
                str_lang = "vi-VN";
            SetLanguage_banhang(str_lang);

            string sSQL = "SELECT * FROM DM_DANGNHAP";
            thuvien othuvien = new thuvien();
            DataTable tbDM_CA_LAM_VIEC = othuvien.SP_SelectDataTable("SP_EXECUTESQL", new string[] { "@sSQL" }, new object[] { sSQL });
            
            txtUsername.Text = new cpublic().Registry_Get(this.Name + txtUsername.Name);
            this.Hide();
            mh_so omh_so;
            switch ( str_lang)
            {
                case "vi-VN":
                     omh_so = new mh_so(true, "Mã đăng nhập");
                     omh_so.eClick += new EventHandler(DangNhap);
                     omh_so.ShowDialog();
                    break;
                case "en-US":
                    omh_so = new mh_so(true, "Enter password");
                    omh_so.eClick += new EventHandler(DangNhap);
                    omh_so.ShowDialog();
                    break;
            }
//            mh_so omh_so = new mh_so(true,"Mã đăng nhập");
            this.Dispose();
            
        }
        void DangNhap(object sender, EventArgs e)
        {
            try
            {
                mh_so omh_so = (mh_so)sender;
                txtUsername.Text = omh_so.txt_ketqua.Text.Trim();

                this.bton_DangNhap_Click(null, null);
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }
        private void bton_1_Click(object sender, EventArgs e)
        {
            txtUsername.Text = new mh_chu().ShowForm("", this.Location.X - 100, this.Location.Y - 100, false);
        }

        private void bton_2_Click(object sender, EventArgs e)
        {
            txtPwd.Text = new mh_chu().ShowForm("", this.Location.X - 100, this.Location.Y - 100, true);
        }

        private void bton_DangNhap_Click(object sender, EventArgs e)
        {
            string _spwd = new cpublic().MaHoa(txtUsername.Text, txtPwd.Text);
            //            
            string sSQL = "SELECT * FROM DM_DANGNHAP";
            thuvien othuvien = new thuvien();
            DataTable tbDM_DANGNHAP = othuvien.SP_SelectDataTable("SP_EXECUTESQL",
                                                                    new string[] { "@sSQl"},
                                                                    new object[] { sSQL});
            
            if (tbDM_DANGNHAP.Select("USERNAME LIKE '" + txtUsername.Text + "'").Length > 0)
            {
                DataRow dr = tbDM_DANGNHAP.Select("USERNAME LIKE '" + txtUsername.Text + "'")[0];

                Variable.username = dr["USERNAME"] + "";
                //Variable. calamviec = dr["OBJCODE"] + "";
                //Variable. calamviec_ten = dr["OBJNAME"] + "";                
                
                this.DialogResult = DialogResult.OK;
                new cpublic().Load_tbBH_CAU_HINH();
                Variable.iquyen = int.Parse("0" + dr["IQUYEN"]);
                this.Dispose();
            }
            else
            {
                Variable.username = "";
                //Variable. calamviec = ""; 1111111

                switch (str_lang)
                {
                    case "vi-VN":
                        str_thongbao = "Mã số không đúng.";
                        break;
                    case "en-US":
                        str_thongbao = "Have password wrong.";
                        break;
                }
                MessageBox_LPV.Show(str_thongbao);
                return;
            }
            new cpublic().Registry_Set(this.Name + txtUsername.Name, txtUsername.Text);
            //new cpublic().Registry_Set(this.Name + cboCaLamViec.Name, cboCaLamViec.Text);

            #region "   CAPNHAT_TONKHO_HANGHOA"
            Boolean bCAPNHAT_TONKHO_HANGHOA = false;
            Boolean.TryParse(Variable.tbBH_CAU_HINH.Rows[0]["CAPNHAT_TONKHO_HANGHOA"] + "", out bCAPNHAT_TONKHO_HANGHOA);
            if (bCAPNHAT_TONKHO_HANGHOA)
            {
                string sTonkhohanghoa = new cpublic().Registry_Get("UPDATETONKHOHANGHOA") + "";
                if (sTonkhohanghoa != DateTime.Now.ToString("ddMMyyyy"))
                {//neu ngay hon nay cap_nhat roi, thi ko cap_nhat nua
                    new thuvien().SP_Update("sp_tonkho_UPDATE", new string[] { "@ma_khohang" }, new object[] { Variable.ma_khohang });
                    new cpublic().Registry_Set("UPDATETONKHOHANGHOA", DateTime.Now.ToString("ddMMyyyy"));
                }
            }
            #endregion
            
            #region "   CAPNHAT_TONKHO_NGUYENLIEU"
            Boolean bCAPNHAT_TONKHO_NGUYENLIEU = false;
            Boolean.TryParse(Variable.tbBH_CAU_HINH.Rows[0]["CAPNHAT_TONKHO_NGUYENLIEU"] + "", out bCAPNHAT_TONKHO_NGUYENLIEU);
            if (bCAPNHAT_TONKHO_NGUYENLIEU)
            {
                string sTonkhoNVL = new cpublic().Registry_Get("UPDATETONKHONVL") + "";
                if (sTonkhoNVL != DateTime.Now.ToString("ddMMyyyy"))
                {//neu ngay hon nay cap_nhat roi, thi ko cap_nhat nua
                    new thuvien().SP_Update("sp_tonkhoNGUYEN_LIEU_UPDATE", new string[] { "@ma_khohang" }, new object[] { Variable.ma_khohang });
                    new cpublic().Registry_Set("UPDATETONKHONVL", DateTime.Now.ToString("ddMMyyyy"));
                }
            }
            #endregion

            #region "   NGAY_LAM_VIEC_LA_NGAY_HE_THONG != True"
            if (Variable.tbBH_CAU_HINH.Rows[0]["NGAY_LAM_VIEC_LA_NGAY_HE_THONG"] + "" != "True")
            {
                if (Variable.ngay_gio_hienhanh.ToString("dd/MM/yyyy") != Variable.ngay_lamviec.ToString("dd/MM/yyyy"))
                {
                    if (Variable.ngay_gio_hienhanh.Hour > 5)
                    {
                        mh_thaydoingaylamviec frm_thaydoingaylamviec = new mh_thaydoingaylamviec();
                        frm_thaydoingaylamviec.ShowDialog();
                    }
                }
            }
            #endregion
        }

        private void bton_thoat_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void txtUsername_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                txtPwd.Focus();
        }

        private void txtPwd_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                cboCaLamViec.Focus();
        }

        private void cboCaLamViec_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                bton_DangNhap.Focus();
        }
    }
}